package com.losdun.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.losdun.product.domain.AttributeEntity;
import com.losdun.product.domain.ProductAttribute;
import com.losdun.product.domain.vo.ProductAttributeVo;
import com.losdun.product.domain.vo.TreeSelectVo;
import com.losdun.product.mapper.ProductAttributeMapper;
import com.losdun.product.service.AttributeEntityService;
import com.losdun.product.service.ProductAttributeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sword.ye
 * @date 2025/3/9
 **/
@Service
public class ProductAttributeServiceImpl extends ServiceImpl<ProductAttributeMapper, ProductAttribute> implements ProductAttributeService {

    @Autowired
    private AttributeEntityService attributeEntityService;

    @Override
    public boolean batchInsert(Long id, List<TreeSelectVo> attrs) {
        if(CollectionUtils.isNotEmpty(attrs) && id!=null){
            List<ProductAttribute> prodAttrs = new ArrayList<>();
            for (TreeSelectVo attr : attrs) {
                if(attr.hasChildren()){
                    List<ProductAttribute> prodAttrList = attr.getChildren().stream().map(t -> {
                        ProductAttribute prodAttr = new ProductAttribute();
                        prodAttr.setProductId(id);
                        prodAttr.setAttributeId(t.getId());
                        prodAttr.setParentAttributeId(attr.getId());
                        return prodAttr;
                    }).collect(Collectors.toList());
                    prodAttrs.addAll(prodAttrList);
                }
            }
            return this.saveBatch(prodAttrs);
        }
        return false;
    }

    @Override
    public void batchUpdate(Long id, List<TreeSelectVo> attrs) {
        if(CollectionUtils.isNotEmpty(attrs) && id!=null){
            removeByProductId(id);
            this.batchInsert(id, attrs);
        }
    }

    @Override
    public List<ProductAttributeVo> queryAttributesByProductId(Long productId) {
        LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAttribute::getProductId, productId);
        List<ProductAttribute> attrsList = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(attrsList)){
            Map<Long, List<ProductAttribute>> groupList = attrsList.stream().collect(Collectors.groupingBy(ProductAttribute::getParentAttributeId));
            List<ProductAttributeVo> prodAttrVoList = new ArrayList<>();
            groupList.forEach((k, v) -> {
                ProductAttributeVo vo=new ProductAttributeVo();
                vo.setAttributeId(k);
                List<Long> attrIds = v.stream().map(ProductAttribute::getAttributeId).collect(Collectors.toList());
                List<AttributeEntity> childrenList = attributeEntityService.listByIds(attrIds);
                vo.setChildrenAttributes(childrenList);
                vo.setAttributeName(attributeEntityService.getById(k).getName());
                prodAttrVoList.add(vo);
            });
            return prodAttrVoList;
        }
        return Collections.emptyList();
    }


    public void removeByProductId(Long productId) {
        LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductAttribute::getProductId, productId);
        this.remove(queryWrapper);
    }
}
